package com.ej.web.controller.ej.mis;

import java.util.Iterator;
import java.util.List;

import com.ej.busi.domain.EjSubject;
import com.ej.busi.service.IEjSubjectService;
import com.ej.common.annotation.Log;
import com.ej.common.core.controller.BaseController;
import com.ej.common.core.domain.AjaxResult;
import com.ej.common.core.domain.entity.SysDept;
import com.ej.common.core.page.TableDataInfo;
import com.ej.common.enums.BusinessType;
import com.ej.common.utils.StringUtils;
import com.ej.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 科目Controller
 * 
 * @author ej
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/ej/mis/subject")
@Api("【科目】Controller")
public class EjSubjectController extends BaseController {
    @Autowired
    private IEjSubjectService ejSubjectService;

    /**
     * 查询科目列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:list')")
    @ApiOperation("查询科目列表")
    @GetMapping("/list")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", dataTypeClass=Integer.class),
        @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", dataTypeClass=Integer.class),
    })
    public AjaxResult list(EjSubject ejSubject)
    {
        List<EjSubject> list = ejSubjectService.selectEjSubjectList(ejSubject);
        return AjaxResult.success(list);
    }


    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list/exclude/{deptId}")
    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
    {
        List<EjSubject> ejSubjects = ejSubjectService.list();
        Iterator<EjSubject> it = ejSubjects.iterator();
        while (it.hasNext())
        {
            EjSubject d = it.next();
            if (d.getId().intValue() == deptId
                    || ArrayUtils.contains(StringUtils.split(d.getParentId(), ","), deptId + ""))
            {
                it.remove();
            }
        }
        return AjaxResult.success(ejSubjects);
    }


    /**
     * 导出科目列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:export')")
    @ApiOperation("导出科目列表")
    @Log(title = "科目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EjSubject ejSubject)
    {
        List<EjSubject> list = ejSubjectService.selectEjSubjectList(ejSubject);
        ExcelUtil<EjSubject> util = new ExcelUtil<EjSubject>(EjSubject.class);
        return util.exportExcel(list, "科目数据");
    }

    /**
     * 获取科目详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:subject:query')")
    @ApiOperation("获取科目信息")
    @ApiImplicitParam(name = "id", value = "科目主键", required = true, dataType = "Long",dataTypeClass=Long.class, paramType = "path")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ejSubjectService.selectEjSubjectById(id));
    }

    /**
     * 新增科目
     */
    @PreAuthorize("@ss.hasPermi('system:subject:add')")
    @ApiOperation("新增科目信息")
    @Log(title = "科目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EjSubject ejSubject)
    {
        return toAjax(ejSubjectService.insertEjSubject(ejSubject));
    }

    /**
     * 修改科目
     */
    @PreAuthorize("@ss.hasPermi('system:subject:edit')")
    @ApiOperation("修改科目信息")
    @Log(title = "科目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EjSubject ejSubject)
    {
        return toAjax(ejSubjectService.updateEjSubject(ejSubject));
    }

    /**
     * 删除科目
     */
    @PreAuthorize("@ss.hasPermi('system:subject:remove')")
    @ApiOperation("删除科目信息")
    @ApiImplicitParam(name = "id", value = "科目主键", required = true, dataType = "Long",dataTypeClass=Long.class, paramType = "path")
    @Log(title = "科目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ejSubjectService.deleteEjSubjectByIds(ids));
    }
}
